package top.hidoctor.auscultation.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.tang.util.DeviceUtil;

import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.bean.ModifyHeadBean;

public class PicAdapter extends RecyclerView.Adapter<PicAdapter.ViewHolder> {
    private Context context;
    private List<ModifyHeadBean> list;
    public onPicItemClick onPicItemClicks;

    public void setOnPicItemClicks(onPicItemClick onPicItemClicks) {
        this.onPicItemClicks = onPicItemClicks;
    }

    public void setList(List<ModifyHeadBean> list) {
        this.list = list;
    }

    public List<ModifyHeadBean> getList() {
        return list;
    }

    public PicAdapter(Context context, List<ModifyHeadBean> piclist) {
        this.context = context;
        this.list = piclist;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_pic, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
                ViewGroup.LayoutParams params = holder.ivPic.getLayoutParams();
        params.height = (new DeviceUtil(context).getWidth() - context.getResources().getDimensionPixelOffset(R.dimen.dp_16) * 2 -
                context.getResources().getDimensionPixelOffset(R.dimen.dp_16) * 4) / 4;
        params.width = (new DeviceUtil(context).getWidth() - context.getResources().getDimensionPixelOffset(R.dimen.dp_16) * 2 -
                context.getResources().getDimensionPixelOffset(R.dimen.dp_16) * 4) / 4;
        holder.ivPic.setLayoutParams(params);
        if (list.size() < getItemCount()) {
            if (position == getItemCount() - 1) {
                Glide.with(context)
                        .load(R.mipmap.ic_add_pic)
                        .into(holder.ivPic);
                holder.ivDelete.setVisibility(View.GONE);
                holder.ivPic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onPicItemClicks.onClick(position);
                    }
                });
            } else {
                Glide.with(context)
                        .load(list.get(position).getFileUrl())
                        .into(holder.ivPic);
                holder.ivDelete.setVisibility(View.VISIBLE);
                holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        list.remove(position);
                        onPicItemClicks.onDelete();
                        notifyDataSetChanged();
                    }
                });
                holder.ivPic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onPicItemClicks.onClick(position);
                    }

                });
            }
        } else {
            Glide.with(context)
                    .load(list.get(position).getFileUrl())
                    .into(holder.ivPic);
            holder.ivDelete.setVisibility(View.VISIBLE);
            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    onPicItemClicks.onDelete();
                    notifyDataSetChanged();
                }
            });
            holder.ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onPicItemClicks.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() == 0) {
            return 1;
        } else if (list.size() < 8) {
            return list.size() + 1;
        } else {
            return 8;
        }
    }

    public void addData(List<ModifyHeadBean> data) {
        list.addAll(data);

    }

    public interface onPicItemClick {

        void onClick(int position);

        void onDelete();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPic;
        ImageView ivDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.iv_pic);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
    }
}
